package com.zhiche.lisa.bms.service.psup.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.dao.mapper.psup.PsupOilpriceMapper;
import com.zhiche.lisa.bms.dao.model.psup.PsupOilprice;
import com.zhiche.lisa.bms.pojo.vo.psup.PsupOilPriceVO;
import com.zhiche.lisa.bms.service.psup.PsupOilpriceService;
import com.zhiche.lisa.bms.utils.ToolCommonUtils;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 人送供方油价维护 服务实现类
 * </p>
 *
 * @author linbao
 * @since 2019-06-18
 */
@Service
@Slf4j
public class PsupOilpriceServiceImpl extends ServiceImpl<PsupOilpriceMapper, PsupOilprice> implements PsupOilpriceService {

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public Page<PsupOilPriceVO> querySdOilPricePage(Page<PsupOilprice> page) {
        EntityWrapper<PsupOilprice> ew = this.buildCondition(page.getCondition());
        List<PsupOilprice> psupOilpriceList = baseMapper.selectPage(page, ew);
        Page<PsupOilPriceVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        if (CollectionUtils.isNotEmpty(psupOilpriceList)) {
            List<PsupOilPriceVO> voList = Lists.newArrayList();
            psupOilpriceList.forEach(oilPrice -> {
                PsupOilPriceVO vo = new PsupOilPriceVO();
                BeanUtils.copyProperties(oilPrice, vo);
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    /**
     * 新增
     *
     * @param psupOilPriceVO
     */
    @Override
    public void createSdOilPrice(PsupOilPriceVO psupOilPriceVO) {
        this.verifyParam(psupOilPriceVO);
        // 校验有效期
        Integer count = verifyEffectiveDate(psupOilPriceVO);
        if (count > 0) {
            throw new BaseException("当前燃油类型的有效期冲突!");
        }
        // 新增
        PsupOilprice createOilPrice = new PsupOilprice();
        BeanUtils.copyProperties(psupOilPriceVO, createOilPrice);
        JwtAccountVO jwtAccount = AccountUtil.getAccountInfoFromSecurityContext();
        createOilPrice.setCreator(Long.valueOf(jwtAccount.getAccountId()));
        createOilPrice.setGmtCreate(new Date());

        PsupOilprice invalidateIsNull = getInvalidateIsNull(createOilPrice);
        if (Objects.nonNull(invalidateIsNull)) {
            invalidateIsNull.setInvalidDate(psupOilPriceVO.getEffectiveDate());
            this.updateById(invalidateIsNull);
        }
        this.insert(createOilPrice);
    }

    /**
     * 校验有效期
     *
     * @param sdOilPriceVO
     * @return
     */
    @Override
    public Integer verifyEffectiveDate(PsupOilPriceVO sdOilPriceVO) {
        return baseMapper.verifyIsExistDate(getMatchAndVerifyCondition(sdOilPriceVO));
    }

    /**
     * 查询匹配对应的市场油价
     *
     * @param sdOilPriceVO
     * @return
     */
    @Override
    public PsupOilprice matchEffectiveDateOilPrice(PsupOilPriceVO sdOilPriceVO) {
        List<PsupOilprice> psupOilpriceList = baseMapper.matchEffectiveDateOilPrice(getMatchAndVerifyCondition(sdOilPriceVO));
        if (CollectionUtils.isNotEmpty(psupOilpriceList)) {
            return psupOilpriceList.get(0);
        }
        return null;
    }

    /**
     * 校验必填项
     *
     * @param psupOilprice
     */
    private void verifyParam(PsupOilPriceVO psupOilprice) {
        if (Objects.isNull(psupOilprice)) {
            throw new BaseException("参数为空");
        }
        if (Objects.isNull(psupOilprice.getOilType())) {
            throw new BaseException("燃油类型为空");
        }
        if (Objects.isNull(psupOilprice.getEffectiveDate())) {
            throw new BaseException("生效日期为空");
        }
        if (Objects.isNull(psupOilprice.getPrice())) {
            throw new BaseException("价格为空");
        }
    }

    /**
     * 查询条件
     *
     * @param condition
     * @return
     */
    private EntityWrapper<PsupOilprice> buildCondition(Map<String, Object> condition) {
        EntityWrapper<PsupOilprice> ew = new EntityWrapper<>();
        ew.orderBy("id desc");
        if (Objects.nonNull(condition)) {
            Object oilType = condition.get("oilType");
            if (Objects.nonNull(oilType) && StringUtils.isNotBlank(oilType.toString())) {
                ew.eq("oil_type", oilType);
            }
            Object effectiveDateObj = condition.get("effectiveDate");
            if (Objects.nonNull(effectiveDateObj) && StringUtils.isNotBlank(effectiveDateObj.toString())) {
                Date effectiveDate = ToolCommonUtils.getDateByStr(effectiveDateObj.toString(), ToolCommonUtils.yyyyMMdd);
                ew.le("effective_date", effectiveDate);
                ew.ge("invalid_date", effectiveDate);
            }
        }
        return ew;
    }


    /**
     * 判断条件
     *
     * @param sdOilPriceVO
     * @return
     */
    private Map<String, Object> getMatchAndVerifyCondition(PsupOilPriceVO sdOilPriceVO) {
        Map<String, Object> condition = Maps.newHashMap();
        if (Objects.nonNull(sdOilPriceVO.getEffectiveDate())) {
            condition.put("effectiveDate", new DateTime(sdOilPriceVO.getEffectiveDate()).toString(ToolCommonUtils.yyyyMMdd));
        }
        condition.put("oilType", sdOilPriceVO.getOilType());
        return condition;
    }


    /**
     * 返回失效时间是空的老数据
     *
     * @param psupOilprice
     * @return
     */
    private PsupOilprice getInvalidateIsNull(PsupOilprice psupOilprice) {
        // 找对最新的
        EntityWrapper<PsupOilprice> ew = new EntityWrapper<>();
        ew.eq("oil_type", psupOilprice.getOilType());
        ew.isNull("invalid_date");
        List<PsupOilprice> psupOilpriceList = this.selectList(ew);
        if (CollectionUtils.isNotEmpty(psupOilpriceList)) {
            return psupOilpriceList.get(0);
        }
        return null;
    }
}
